package com.example.fbi_text.bean;

import android.os.Parcel;
import android.os.Parcelable;

//购物车实体类
public class ProductCart implements Parcelable {
    private int cartID;             // 购物车项ID
    private String cartName;        // 商品名称
    private String cartPrice;       // 商品价格
    private String cartDescription; // 商品描述
    private String cartReviews;     // 商品评价数
    private int imageResId;         // 商品图片资源ID
    private int quantity = 1;  // 新增数量字段

    // 默认构造方法
    public ProductCart() {
    }

    public int getPriceAsInt() {
        try {
            return Integer.parseInt(cartPrice);
        } catch (NumberFormatException e) {
            return 0;
        }
    }
    // 全参数构造方法
    public ProductCart(int cartID, String cartName, String cartPrice,
                       String cartDescription, String cartReviews, int imageResId) {
        this.cartID = cartID;
        this.cartName = cartName;
        this.cartPrice = cartPrice;
        this.cartDescription = cartDescription;
        this.cartReviews = cartReviews;
        this.imageResId = imageResId;
        this.quantity = quantity;
    }

    // Parcelable实现（用于Activity间传递对象）
    protected ProductCart(Parcel in) {
        cartID = in.readInt();
        cartName = in.readString();
        cartPrice = in.readString();
        cartDescription = in.readString();
        cartReviews = in.readString();
        imageResId = in.readInt();
        quantity = in.readInt();
    }

    public static final Creator<ProductCart> CREATOR = new Creator<ProductCart>() {
        @Override
        public ProductCart createFromParcel(Parcel in) {
            return new ProductCart(in);
        }

        @Override
        public ProductCart[] newArray(int size) {
            return new ProductCart[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(cartID);
        dest.writeString(cartName);
        dest.writeString(cartPrice);
        dest.writeString(cartDescription);
        dest.writeString(cartReviews);
        dest.writeInt(imageResId);
        dest.writeInt(quantity);
    }

    // Getter和Setter方法
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    public int getCartID() {
        return cartID;
    }

    public void setCartID(int cartID) {
        this.cartID = cartID;
    }

    public String getCartName() {
        return cartName;
    }

    public void setCartName(String cartName) {
        this.cartName = cartName;
    }

    public String getCartPrice() {
        return cartPrice;
    }

    public void setCartPrice(String cartPrice) {
        this.cartPrice = cartPrice;
    }

    public String getCartDescription() {
        return cartDescription;
    }

    public void setCartDescription(String cartDescription) {
        this.cartDescription = cartDescription;
    }

    public String getCartReviews() {
        return cartReviews;
    }

    public void setCartReviews(String cartReviews) {
        this.cartReviews = cartReviews;
    }

    public int getImageResId() {
        return imageResId;
    }

    public void setImageResId(int imageResId) {
        this.imageResId = imageResId;
    }

    @Override
    public String toString() {
        return "ProductCart{" +
                "cartID=" + cartID +
                ", cartName='" + cartName + '\'' +
                ", cartPrice='" + cartPrice + '\'' +
                ", cartDescription='" + cartDescription + '\'' +
                ", cartReviews='" + cartReviews + '\'' +
                ", imageResId=" + imageResId +
                '}';
    }

    public void increaseQuantity() {
        quantity++;
    }

    public void decreaseQuantity() {
        if (quantity > 1) {
            quantity--;
        }
    }
}
